package com.chenyun.message.sms.service.client.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.chenyun.message.sms.config.SmsConfig;
import com.chenyun.message.sms.dto.SmsResponseDTO;
import com.chenyun.message.sms.dto.SmsSendReqDTO;
import com.chenyun.message.sms.dto.SmsTemplateReqDTO;
import com.chenyun.message.sms.service.client.ISmsClient;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import lombok.SneakyThrows;

/**
 * 腾讯短信实现类
 * @author: Lonni
 * @date: 2022/8/2 0002 14:00
 */
public class TencentSmsClient implements ISmsClient {
    private static  volatile SmsClient smsClient;
    //腾讯发送短信需要appid
    private static String TENCENT_SMS_APP_ID="";

    /**
     * 初始化操作
     *
     * @param smsConfig
     */
    @Override
    public void init(SmsConfig smsConfig) {
        // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(smsConfig.getAccessKey(), smsConfig.getAccessSecret());
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(smsConfig.getEndpoint());
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        smsClient = new SmsClient(cred, smsConfig.getRegionId(), clientProfile);
        TENCENT_SMS_APP_ID=smsConfig.getAppId();
    }

    /**
     * 发送短信
     *
     * @param requestDTO
     * @return
     */
    @SneakyThrows
    @Override
    public SmsResponseDTO<SmsResponseDTO.SmsSendRsp> doSendSms(SmsSendReqDTO requestDTO) {
        SendSmsRequest req = new SendSmsRequest();
        req.setPhoneNumberSet(requestDTO.getPhoneNumber().split(","));
        req.setSmsSdkAppId(TENCENT_SMS_APP_ID);
        req.setSignName(requestDTO.getSignName());
        req.setTemplateId(requestDTO.getTemplateCode());
        if (CollectionUtil.isNotEmpty(requestDTO.getTemplateParam())){
            String[] params = requestDTO.getTemplateParam().values().toArray(new String[]{});
            req.setTemplateParamSet(params);
        }
        SendSmsResponse resp = smsClient.SendSms(req);
        SendStatus sendStatus = resp.getSendStatusSet()[0];
        SmsResponseDTO.SmsSendRsp smsSendRsp=SmsResponseDTO.SmsSendRsp.builder()
                .code(sendStatus.getCode())
                .message(sendStatus.getMessage())
                .requestId(resp.getRequestId())
                .bizId(sendStatus.getSerialNo())
                .build();
        return  SmsResponseDTO.<SmsResponseDTO.SmsSendRsp>builder()
                .success(sendStatus.getCode().equals("OK"))
                .msg(sendStatus.getMessage())
                .data(smsSendRsp)
                .build();
    }

    /**
     * 同步||修改短信模板
     * 修改时templateCode不能为空
     *
     * @param templateDTO
     * @return
     */
    @Override
    public SmsResponseDTO<SmsResponseDTO.SmsAddTemplateRsp> syncSmsTemplate(SmsTemplateReqDTO templateDTO) {
        return null;
    }

    /**
     * 查询短信模板的审核状态
     * @param templateCode
     * @return
     */
    @Override
    public SmsResponseDTO<SmsResponseDTO.SmsTemplateStatusRsp> querySmsTemplateStatus(String templateCode) {
        return null;
    }

    /**
     * 删除短信模版
     * @param templateCode
     * @return
     */
    @SneakyThrows
    @Override
    public SmsResponseDTO<SmsResponseDTO.SmsAddTemplateRsp> delSmsTemplate(String templateCode) {
        DeleteSmsTemplateRequest req = new DeleteSmsTemplateRequest();
        req.setTemplateId(Long.parseLong(templateCode));
        // 返回的resp是一个DeleteSmsTemplateResponse的实例，与请求对象对应
        DeleteSmsTemplateResponse resp = smsClient.DeleteSmsTemplate(req);
        DeleteTemplateStatus deleteTemplateStatus = resp.getDeleteTemplateStatus();
        return  SmsResponseDTO.<SmsResponseDTO.SmsAddTemplateRsp>builder()
                .success(deleteTemplateStatus.getDeleteStatus().equals("return successfully!"))
                .build();

    }
}
